<template>
  <div style="background: #f7f8fa ">
    <we-nav-bar></we-nav-bar>
    <div style="margin-bottom: 150px">
      <van-row>
        <van-col span="24">
          <div style="margin-left: 1rem">
            <p>当前 : {{ now.name }}</p>
          </div>
        </van-col>
      </van-row>
      <p class="comTitle">待支付处方</p>
      <ToPayForPrescription
        v-for="val in dataLists"
        :key="val.AdmRowID"
        :amount="val.PayMoney"
        :clinicaltime="val.AdmDate.substr(0, 16)"
        :clinicdepartment="val.AdmDept"
        :clinicdoctor="val.AdmDoct"
        :state="val.check_status"
        @checkPrescribingInformation="getInfo(val.AdmRowID)"
        @payprescription="payFunc(val)"
        :AdmRowID="val.AdmRowID"
        @state-change="getNewState"
      ></ToPayForPrescription>

      <pay-foot
        :isFull="fullstate"
        @full-change="getFullState"
        @mergepayment="mergerpayment"
        :amount="amt"
        :isShowbottom="this.dataLists.length > 1"
      ></pay-foot>
      <div v-if="dataLists.length === 0">
        <van-row>
          <van-col span="24">
            <div
              style="text-align: center;height: 6vh;background: white;line-height: 6vh"
            >
              未查询到待支付处方 . . .
            </div>
          </van-col>
        </van-row>
      </div>
      <p class="comTitle">已支付处方</p>
      <div v-if="payed">
      <div v-for="val in payed" :key="val.id">
        <van-row>
          <van-col span="24">
            <van-panel
              style="margin-top: 1rem;padding: 5px 8px;"
              :title="'金额 : ' + val.pay_money + '元'"
              :desc="'就诊时间 : ' + val.adm_date.substr(0, 16)"
              status="已支付"
            >
              <div style="padding: 20px;">
                <div>就诊科室 : {{ val.adm_dept }}</div>
                <div>就诊医生 : {{ val.adm_doct }}</div>
              </div>
              <div slot="footer">
                <van-row>
                  <van-col @click="getInfo(val.row_id)" span="5" offset="19">
                    <van-button size="small">查看</van-button>
                  </van-col>
                </van-row>
              </div>
            </van-panel>
          </van-col>
        </van-row>
      </div>
      </div>
      <van-row>
        <van-col>
          <van-action-sheet
            v-model="show"
            :actions="users"
            @select="onSelect"
          />
        </van-col>
      </van-row>
    </div>
    <div
      class="btn-block"
      style="background: white;margin: 0;width: 100%;padding-top: 8px;text-align: center"
    >
      <van-row>
        <van-col span="22" offset="1">
          <van-button
            type="info"
            @click="selectUser"
            style="border-radius: 0.2rem;overflow: hidden"
            size="large"
          >
            切换就诊人
          </van-button>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import ToPayForPrescription from "@/components/ToPayForPrescription";
import PayFoot from "@/components/PayFoot";
import { Dialog } from 'vant';
export default {
  name: "zjzflist",
  components: {
    ToPayForPrescription,
    PayFoot,
  },
  data() {
    return {
      url: "/app/wechat/Zjzf/",
      dataLists: [],
      payed: [],
      show: false,
      users: [],
      now: {
        name: "",
        id_code: "",
      },
    };
  },
  computed: {
    fullstate() {
      return this.dataLists.every((item) => item.check_status);
    },
    amt() {
      return this.dataLists
        .filter((item) => item.check_status)
        .reduce((total, item) => (total += (Number(item.PayMoney)*100)), 0)/100;
    },
  },
  mounted() {
    this.currentMan();
    this.getUsers();
  },
  methods: {
    //当前患者
    currentMan() {
      if (
        window.localStorage.getItem("current_patient_name") &&
        window.localStorage.getItem("current_patient_ID")
      ) {
        this.now.name = window.localStorage.getItem("current_patient_name");
        this.now.id_code = window.localStorage.getItem("current_patient_ID");
        this.getList(this.now);
        this.getPayed(this.now.id_code);
      } else {
        this.getLoginUser();
      }
    },
    //获取未支付处方
    getList(data) {
      this.axios
        .post(this.url + "lists/mid/2.html", { type: 1, data: data })
        .then(({ data }) => {
          if (data.ResultCode === "0" && data.Items !== undefined) {
            if (data.Items.Item[0] !== undefined) {
              this.dataLists = data.Items.Item;
              for (var i = 0; i < this.dataLists.length; i++) {
                this.$set(this.dataLists[i], "check_status", false);
              }
              console.log(this.dataLists);
            } else {
              this.dataLists = [data.Items.Item];
            }
          } else {
            this.dataLists = [];
          }
        });
    },
    getFullState(val) {
      console.log("在父组件中拿到了状态");
      this.dataLists.forEach((item) => (item.check_status = val));
    },
    //获取已支付处方
    getPayed(idCode) {
      this.axios
        .post(this.url + "getPayed/mid/2.html", { id_code: idCode })
        .then(({ data }) => {
          if (data.code === 0) {
            this.payed = data.data;
          } else {
            this.payed = [];
          }
        });
    },
    getLoginUser() {
      this.axios.post(this.url + "loguser/mid/2.html").then(({ data }) => {
        if (data.code === 0) {
          this.now = data.data;
          this.getList(this.now);
          this.getPayed(this.now.id_code);
        }
      });
    },
    //获取用户列表
    getUsers() {
      this.axios
        .post("/app/wechat/record/huanzhe/mid/2.html")
        .then(({ data }) => {
          if (data.error === 0) {
            this.users = data.info;
          }
        });
    },
    //点击按钮
    selectUser() {
      this.show = !this.show;
    },

    getNewState(vals) {
      console.log(vals)
      this.dataLists.some((item) => {
        if (item.AdmRowID === vals.AdmRowID) {
          item.check_status = vals.value;
          return true;
        }
      });
    },
    //选择用户
    onSelect(item) {
      this.now.name = item.name;
      this.now.id_code = item.id_code;
      window.localStorage.setItem("current_patient_name", item.name);
      window.localStorage.setItem("current_patient_ID", item.id_code);
      this.getList(this.now);
      this.getPayed(this.now.id_code);
      this.show = false;
    },
    getInfo(AdmRowID) {
      this.$router.push({
        path: "/wechat/zjzfinfo",
        query: { AdmRowID: AdmRowID },
      });
    },
    payFunc(val) {
      val.id_code = this.now.id_code;
      this.axios
        .post(this.url + "placeOrder/mid/2.html", { data: val })
        .then(({ data }) => {
          console.log(data);
          if (data.code === 0) {
            let url =
              "/app/wechat/Pay/orderPay/mid/2.html?money=" +
              val.PayMoney +
              "&OrderID=" +
              data.data.order_code +
              "&title=诊间支付&callback=zjzfCall";
             window.location.href = url;
          }
        });
    },

    mergerpayment() {
     const copydataLists = this.dataLists.filter(item=>item.check_status===true)
      let arr = []
      copydataLists.forEach(e => {
        e.id_code = this.now.id_code
        arr.push(e)
      })
     console.log(arr)
      for(let i = 0; i < arr.length-1; i++) {
        if(arr[i].OrderID !== arr[i+1].OrderID) {
          Dialog.alert({
          title: '',
          message: '您的待支付项不在一个单号，请分别支付。',
        })
       return false
        }
      }

     const parameters_the_array = JSON.stringify(arr)
      this.axios
        .post(this.url + "allplaceOrder/mid/2.html", { data: parameters_the_array })
        .then(({ data }) => {
          if (data.code === 0) {
            let url =
              "/app/wechat/Pay/orderPay/mid/2.html?money=" +
              this.amt +
              "&OrderID=" +
              data.data.order_code +
              "&title=诊间支付&callback=zjzfCall";
             window.location.href = url;
          }
        });

    },
  },
};
</script>

<style scoped>
.btn-block {
  position: fixed;
  bottom: 5px;
}

.comTitle {
  height: 6vh;
  font-size: 1rem;
  line-height: 6vh;
  padding: 0 0.1rem;
  border-bottom: 1px solid #eee;
  color: #aaa;
}


</style>
